//
//  AppDelegate.swift
//  77Club
//
//  Created by Apple on 2024/8/26.
//

import UIKit
@_exported import Stevia
@_exported import KakaJSON
@_exported import IQKeyboardManager


@main
class AppDelegate: UIResponder, UIApplicationDelegate, WXApiDelegate, TencentSessionDelegate {
    
    func tencentDidLogin() {
        
    }
    
    func tencentDidNotLogin(_ cancelled: Bool) {
        
    }
    
    func tencentDidNotNetWork() {
        
    }
    

    var window: UIWindow?
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        initializeWindow()
        configUM()
        
//        let tencentOAuth = TencentOAuth(appId: "102278721", andDelegate: nil)
        TencentOAuth().setupAppId("102278721", enableUniveralLink: true, universalLink: "https://www.77.top/qq_conn/102278721", delegate: self)
        WXApi.registerApp("wx4bc1c702f9475edf", universalLink: "https://www.77.top/app/path/to/content/")
        
        return true
    }
    
//    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
//        return UMSocialManager.default().handleOpen(url)
//    }

    private func initializeWindow(){
        window = UIWindow(frame: UIScreen.main.bounds)

        let launchVC = ViewController()
        launchVC.completion = {
            self.window?.rootViewController = ClubTabBarViewController()
        }
        window?.rootViewController = launchVC
        window?.makeKeyAndVisible()
    }

    private func configUM(){
        
//        // 友盟初始化
//        UMConfigure.setLogEnabled(true)  // 开启友盟日志
//        UMConfigure.initWithAppkey("67248e7d8f232a05f1a6d036", channel: "App Store")
//        // 配置微信平台
//        UMSocialManager.default().setPlaform(.wechatSession, appKey: "wx4bc1c702f9475edf", appSecret: nil, redirectURL: "https://www.77.top/app/path/to/content/")
    }
    
    func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
        if url.scheme == "77top" {
            print("Custom URL Scheme detected: \(url)")
            // 根据 URL 处理逻辑
            handleCustomURL(url)
            return true
        }else if url.scheme?.hasPrefix("tencent") == true {
            // QQ 回调
            return TencentOAuth.handleOpen(url)
        }
        // 处理微信的回调
        return WXApi.handleOpen(url, delegate: self)
    }

    func application(_ application: UIApplication, handleOpen url: URL) -> Bool {
        return TencentOAuth.handleOpen(url)
    }

    func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
        return TencentOAuth.handleOpen(url)
    }

    
    //如果支持 Universal Links
    func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
        return WXApi.handleOpenUniversalLink(userActivity, delegate: self)
    }

    // 实现微信回调
    func onResp(_ resp: BaseResp) {
        if let sendResp = resp as? SendMessageToWXResp {
            if sendResp.errCode == WXSuccess.rawValue {
                print("分享成功")
            } else {
                print("分享失败，错误代码：\(sendResp.errCode)")
            }
        }
    }
    

    private func handleCustomURL(_ url: URL) {
        // 解析 URL 并处理
        if let host = url.host {
            print("Host: \(host)")
        }
        if let queryItems = URLComponents(url: url, resolvingAgainstBaseURL: false)?.queryItems {
            for item in queryItems {
                print("\(item.name): \(item.value ?? "")")
            }
        }
    }

}

